# encoding: utf-8

class ApplicationController < ActionController::Base
  protect_from_forgery

  def current_user
    @current_user ||= Account.where("_id" => session[:current_user]).first
    @current_user ||= Account.where("_id" => cookies[:flowy_token]).first
    @current_user
  end

  def account_access
    if current_user
      return true
    else
      render_msg :type => :error, :msg => "尚未登录"
      return false
    end
  end

  def render_msg(options)
    retval = {
      :error => nil,
      :msg   => {}
    }
    case options[:type]
    when :error
      retval[:error]         = options[:error]
      retval[:msg][:type]    = :error
      retval[:msg][:content] = "<strong>错误:</strong>#{options[:msg]}"
    when :success
      retval[:error]        = ""
      retval[:msg][:type]    = :success
      retval[:msg][:content] = options[:msg]
    else
      retval[:error] = ""
      retval[:msg][:type]    = :error
    end
    render :json => retval
  end


  def render_json(type = nil , content = "")
    retval = {
      :error => type == :error ? true : nil,
      :msg   => {
        :type    => type,
        :content => content
        },
      :value => block_given? ? yield : ''
    }

    render :json => retval
  end

  def trim_params
    params.each do |k,v|
      unless v.present?
        params.delete k
      end
    end
  end



end
